﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Hafic.Stock.Management.BLL;
using Hafic.Stock.Management.Entity;
using Hafic.Stock.Management.Common;

namespace Hafic.Stock.Management.Security
{
    public partial class frmHandJob : DevExpress.XtraEditors.XtraForm
    {
        #region "Define Variable"
        private clsEmployee objEmployee = new clsEmployee();
        private Employee _report;

        #endregion

        #region "Construction"
        public frmHandJob()
        {
            InitializeComponent();
            getEmployee();
        }
        #endregion

        #region "Private Method"

        private void getEmployee()
        {
            DataTable dtEmployee = objEmployee.SelectAll();
            txtEmployee.Properties.DataSource = dtEmployee;
            txtEmployee.Properties.DisplayMember = "FullName";
            txtEmployee.Properties.ValueMember = "ID";

            txtEmployeeTo.Properties.DataSource = dtEmployee;
            txtEmployeeTo.Properties.DisplayMember = "FullName";
            txtEmployeeTo.Properties.ValueMember = "ID";
        }

        private void doHandJob()
        {
            #region "Check Condition"
            if (txtEmployee.EditValue==null)
            {
                XtraMessageBox.Show(
                    "Người bàn giao không được để trống.",
                    "Thông báo",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                txtEmployee.SelectAll();
                txtEmployee.Focus();
                return;
            }
            
            if (txtEmployeeTo.EditValue == null)
            {
                XtraMessageBox.Show(
                    "Người được bàn giao không được để trống.",
                    "Thông báo",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                txtEmployeeTo.SelectAll();
                txtEmployeeTo.Focus();
                return;
            }
            #endregion

            _report = new Employee();
            _report.Id = (int)txtEmployee.EditValue;
            _report.EmployeeToId = (int)txtEmployeeTo.EditValue;
            _report.FromDate = txtFromDate.DateTime;
            _report.ToDate = txtToDate.DateTime;
            objEmployee.Report = _report;

            try
            {
                objEmployee.HandJob();
                XtraMessageBox.Show(
                            "Công việc đã được bàn giao.", 
                            "Thông báo", 
                            MessageBoxButtons.OK, 
                            MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(
                            "Có lỗi xảy ra trong quá trình bàn giao công việc: " + ex.Message,
                            "Thông báo",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
                return;
            }
        }
        #endregion

        #region "Form Event"
        private void frmHandJob_Load(object sender, EventArgs e)
        {
            if (clsVariable._employeeId>0)
            {
                txtEmployee.EditValue = clsVariable._employeeId;
            }
        }

        #endregion

        #region "Button Event"
        private void btnOk_Click(object sender, EventArgs e)
        {
            doHandJob();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

    }
}